package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.MyResult;
import com.api.shopapi.entity.PreCheck;
import com.api.shopapi.entity.NursingLevelPrice;
import com.api.shopapi.service.NursingLevelPriceService;
import com.api.shopapi.service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * @author maochuan
 * 房间
 */
@RestController
@RequestMapping("NursingLevelPrice")
public class NursingLevelPriceController extends BaseController {

    @Autowired
    @Qualifier("NursingLevelPriceServiceImplOne")
    private NursingLevelPriceService nursingLevelPriceService;

    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;


    @RequestMapping(value = "Add", method = RequestMethod.POST)
    public MyResult Insert(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            String nursingLevelCode = jsonObject.getString("nursingLevelCode");
            String calculationType = jsonObject.getString("calculationType");
            double price = jsonObject.getDouble("price");
            PreCheck preCheck = checkSignAdmInterface(timeStr,sign,staffCode,"/NursingLevelPrice/Add");
            if(preCheck.getError() == 1){
                return failed(preCheck.getMessage());
            }
            NursingLevelPrice nursingLevelPrice = nursingLevelPriceService.getByNursingLevelCodeAndCalculationType(nursingLevelCode,calculationType);
            if(nursingLevelPrice == null){
                nursingLevelPrice = new NursingLevelPrice();
                nursingLevelPrice.setNursingLevelPriceCode(UUID.randomUUID().toString());
                nursingLevelPrice.setNursingLevelCode(nursingLevelCode);
                nursingLevelPrice.setPrice(price);
                nursingLevelPrice.setCalculationType(calculationType);
                nursingLevelPriceService.insert(nursingLevelPrice);
                Object data = new Object[]{};
                return success(data);
            }else {
                return failed("价格已存在");
            }
        }catch (Exception e){
                insertError("/NursingLevelPrice/Add", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

    @RequestMapping(value = "Edit", method = RequestMethod.POST)
    public MyResult Edit(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            double price = jsonObject.getDouble("price");
            String nursingLevelCode = jsonObject.getString("nursingLevelCode");
            String calculationType = jsonObject.getString("calculationType");
            String nursingLevelPriceCode = jsonObject.getString("nursingLevelPriceCode");
            PreCheck preCheck = checkSignAdmInterface(timeStr,sign,staffCode,"/NursingLevelPrice/Edit");
            if(preCheck.getError() == 1){
                return failed(preCheck.getMessage());
            }
            NursingLevelPrice nursingLevelPrice = nursingLevelPriceService.getByNursingLevelCodeAndCalculationType(nursingLevelCode,calculationType);
            if(nursingLevelPrice!=null && !nursingLevelPrice.getNursingLevelPriceCode().equals(nursingLevelPriceCode)){
                return failed("价格已存在");
            }else {
                nursingLevelPrice = nursingLevelPriceService.getByNursingLevelPriceCode(nursingLevelPriceCode);
            }
            nursingLevelPrice.setPrice(price);
            nursingLevelPrice.setNursingLevelCode(nursingLevelCode);
            nursingLevelPrice.setCalculationType(calculationType);
            nursingLevelPriceService.updateByNursingLevelPriceCode(nursingLevelPrice);
            Object data = new Object[]{};
            return success(data);

        }catch (Exception e){
            insertError("/NursingLevelPrice/Edit", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }

}
